//
//  PicturesView.swift
//  weiboSwift1.0
//
//  Created by xuNick on 16/4/22.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit

let KpicMagin:CGFloat = 5

let Kscale:CGFloat = UIScreen.mainScreen().scale

//计算item大小
let itemWH:CGFloat = {
    let w = Int(UIScreen.mainScreen().bounds.width - 2 * KpicMagin - 2 * CellMagin) / 3
    let wh = CGFloat(w)
    return wh
}()

class PicturesView: UICollectionView {
    
    
    var h:CGFloat = 0

    var pics:[UIImageView]?{
        didSet{
            setupUI()
            self.reloadData()
            
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        self.dataSource = self
        setupUI()
        self.backgroundColor = UIColor.whiteColor()
        registerClass(PicCell.self, forCellWithReuseIdentifier: "reuse")
        self.userInteractionEnabled = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//        
//        //获取layout对象
//        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
//        //拿数组个数
//        let count = pics?.count ?? 0
//        
//        //如果是单图的话,改变itemsize大小
//        if count == 1 {
//            print(pics?.first?.image?.size)
//            layout.itemSize = (pics?.first?.image?.size) ?? CGSizeZero
//            
//            h = pics?.first?.image?.size.height ?? 0
//        }
//
//    }
    //MARK: - 界面设置
    func setupUI(){
        //获取layout对象
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //拿数组个数
        let count = pics?.count ?? 0
       
        
        let row = count == 4 ? 2 : (count <= 0 ? 0 : ((count - 1) / 3 + 1))
            //        let col = count == 4 ? 2 : 3
       
        
        //设置layout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        
//        //如果是单图的话,改变itemsize大小
//        if count == 1 {
//            
//            layout.itemSize = (pics?.first?.image?.size) ?? CGSizeZero
//        }
        
        
        layout.minimumLineSpacing = KpicMagin
        layout.minimumInteritemSpacing = KpicMagin
        self.contentInset = UIEdgeInsetsMake(5, 5, 5, 5)
        //设置view的高度
//        self.snp_makeConstraints { (make) -> Void in
//            make.height.equalTo(CGFloat(row) * itemWH + 2 * CellMagin + CGFloat(row - 1) * KpicMagin)
//        }
        h = count > 0 ? CGFloat(row) * itemWH + 2 * CellMagin + CGFloat(row - 1) * KpicMagin : 0
        if count == 1 {
                       
                        layout.itemSize = (pics?.first?.image?.size) ?? CGSizeZero
                        layout.itemSize = CGSize(width: layout.itemSize.width * Kscale, height: layout.itemSize.height * Kscale)
            
                        h = pics?.first?.image?.size.height ?? 0
                        h = h * Kscale + 10
                    }

     
    }
}

extension PicturesView:UICollectionViewDataSource {
    
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
//        print(pics?.count ?? 0)
        return pics?.count ?? 0
//        return 2
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
         let cell = collectionView.dequeueReusableCellWithReuseIdentifier("reuse", forIndexPath: indexPath) as! PicCell
        cell.image = pics![indexPath.row]
        
        cell.backgroundColor = UIColor.redColor()
        return cell
    }
    
   

    
    
}
